// import { graphqlKoa, graphiqlKoa } from 'graphql-server-koa'
// import {saveInfo, fetchInfo} from '../controllers/info'
// import {saveStudent, fetchStudent, fetchStudentDetail} from '../controllers/student'
//
//
// const router = require('koa-router')()
//
// router.post('/saveinfo', saveInfo)
//     .get('/info', fetchInfo)
//     .post('/savestudent', saveStudent)
//     .get('/student', fetchStudent)
//     .get('/studentDetail', fetchStudentDetail)
//     .get('/graphiql', async (ctx, next) => {
//         await graphiqlKoa({endpointURL: '/graphql'})(ctx, next)
//     })
// module.exports = router

import { graphqlKoa, graphiqlKoa } from 'graphql-server-koa'
import {saveInfo, fetchInfo} from '../controllers/info'
import {saveStudent, fetchStudent, fetchStudentDetail} from '../controllers/student'

// 引入schema
import schema from '../graphql/schema'

const router = require('koa-router')()

router.post('/saveinfo', saveInfo)
    .get('/info', fetchInfo)
    .post('/savestudent', saveStudent)
    .get('/student', fetchStudent)
    .get('/studentDetail', fetchStudentDetail)


// restful
let data = {}
router
    .get('/api/v1/student',async(ctx,next) => {
        // let data = await Sql.queryAll(tbName,ctx.request.query);
        ctx.body = data;
    })
    .get('/api/v1/student/:id',async(ctx,next) => {
        // let data = await Sql.query(tbName,ctx.params.id);
        ctx.body = data;
    })
    .post('/api/v1/student',async(ctx,next) => {
        // let data = await Sql.insert(tbName,ctx.request.body);
        ctx.body = data;
    })
    .patch('/api/v1/student/rows',async(ctx,next) => {
        // let data = await Sql.insertRows(tbName,ctx.request.body);
        ctx.body = data;
    })
    .put('/api/v1/student/:id',async(ctx,next) => {
        // let data = await Sql.update(tbName,ctx.params.id,ctx.request.body);
        ctx.body = data;
    })
    .put('/api/v1/student',async(ctx,next) => {
        console.log(ctx.request.body);
        // let data = await Sql.updateRows(tbName,ctx.request.body);
        ctx.body = data;
    })
    .del('/api/v1/student/:id',async(ctx,next) => {
        // let data = await Sql.delete(tbName,ctx.params.id);
        ctx.body = data;
    })
    .del('/api/v1/student',async(ctx,next) => {
        // let data = await Sql.deleteRows(tbName,ctx.request.body);
        ctx.body = data;
    })


router.post('/graphql', async (ctx, next) => {
    await graphqlKoa({schema: schema})(ctx, next) // 使用schema
})
    .get('/graphql', async (ctx, next) => {
        await graphqlKoa({schema: schema})(ctx, next) // 使用schema
    })
    .get('/graphiql', async (ctx, next) => {
        await graphiqlKoa({endpointURL: '/graphql'})(ctx, next) // 重定向到graphiql路由
    })
module.exports = router
